# -*- coding: utf-8 -*-
import requests
from lxml import etree
import time
import random

tencent_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]
for nums in range(0,5010,30):
    url = 'https://v.qq.com/x/bu/pagesheet/list?_all=1&append=1&channel=movie&listpage=2&offset={0}'.format(nums)
    headers = {
        'User-Agent': random.choice(tencent_headers),
        'Cookie': 'pgv_pvi=7083304960; RK=5pjZ216Isa; ptcz=7df04ce78c7e93c15f68f9e5a492ddad97fd80135c9c35cd4239e9eeb902561f; pgv_pvid=9933966504; pac_uid=0_44b35919aed6e; pgv_si=s326039552; _qpsvr_localtk=0.5265509683112038; pgv_info=ssid=s8634295344; ptag=www_baidu_com|channel; tvfe_boss_uuid=6001708de388c970; ad_play_index=17; video_guid=4e1993d398c6c7e3; video_platform=2; bucket_id=9231000; ts_last=v.qq.com/channel/movie; ts_refer=www.baidu.com/link; ts_uid=3120220338; qv_als=DWU0Qzjq/5Q4CIsjA11581395722jw1Qfg=='
    }
    response = requests.get(url, headers=headers)
    html = response.content.decode('utf8')
    HTML = etree.HTML(html)
    minute = HTML.xpath('//div[@class="figure_caption"]/text()')
    with open("1.txt", "a", encoding='utf8') as file:
        for hour_time in minute:
            try:
                hour = int(hour_time[:-6])
                allmintue = int(hour_time[3:-3])
                comment_data = hour*60 + allmintue +1
                file.write(str(comment_data)+'\n')
            except:
                pass
    print("第{0}页爬取成功".format((nums//30)+1))
    time.sleep(0.5)


